local dianshou = fk.CreateSkill {
  name = "lingling__dianshou",
  tags = { Skill.Switch }
}

Fk:loadTranslationTable {
  ["lingling__dianshou"] = "点授",
  [":lingling__dianshou"] = "转换技，当你受到伤害后，你可以摸一张牌，然后①令伤害来源弃置两张牌；②与伤害来源各摸两张牌。攻击范围内有你的其他角色回合开始时，你可以重铸一张牌转换此技能。",

  [":lingling__dianshou_yang"] = "转换技，当你受到伤害后，你可以摸一张牌，然后<font color=\"#E0DB2F\">①令伤害来源弃置两张牌；</font>②与伤害来源各摸两张牌。攻击范围内有你的其他角色回合开始时，你可以重铸一张牌转换此技能。",
  [":lingling__dianshou_yin"] = "转换技，当你受到伤害后，你可以摸一张牌，然后①令伤害来源弃置两张牌；<font color=\"#E0DB2F\">②与伤害来源各摸两张牌。</font>攻击范围内有你的其他角色回合开始时，你可以重铸一张牌转换此技能。",

  ["#lingling__dianshou-invoke-yang"] = "点授：你可以摸一张牌，令 %src 弃置两张牌",
  ["#lingling__dianshou-invoke-yin"] = "点授：你可以摸一张牌，与 %src 各摸两张牌",
  ["#lingling__dianshou-invoke"] = "点授：你可以重铸一张牌",
}

dianshou:addEffect(fk.Damaged, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(dianshou.name) and data.from and data.from:isAlive()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(dianshou.name) == fk.SwitchYang then
      return room:askToSkillInvoke(player, {
        skill_name = dianshou.name,
        prompt = "#lingling__dianshou-invoke-yang:" .. data.from.id
      })
    elseif player:getSwitchSkillState(dianshou.name) == fk.SwitchYin then
      return room:askToSkillInvoke(player, {
        skill_name = dianshou.name,
        prompt = "#lingling__dianshou-invoke-yin:" .. data.from.id
      })
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.dead then return end
    player:drawCards(1, dianshou.name)
    room:doIndicate(player, { data.from })
    if player:getSwitchSkillState(dianshou.name, true) == fk.SwitchYang and not data.from.dead then
      if #data.from:getCardIds("he") > 2 then
        room:askToDiscard(data.from, {
          min_num = 2,
          max_num = 2,
          include_equip = true,
          cancelable = false,
          skill_name = dianshou.name
        })
      else
        data.from:throwAllCards("he", dianshou.name)
      end
    elseif player:getSwitchSkillState(dianshou.name, true) == fk.SwitchYin and not player.dead then
      player:drawCards(2, dianshou.name)
      data.from:drawCards(2, dianshou.name)
    end
  end
})

dianshou:addEffect(fk.TurnStart,{
  anim_type = "switch",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(dianshou.name) and target ~= player and target:inMyAttackRange(player)
  end,
  on_cost = function (self, event, target, player, data)
    local card = player.room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = dianshou.name,
      cancelable = true,
      prompt = "#lingling__dianshou-invoke",
    })
    if #card > 0 then
      event:setCostData(self, {cards = card})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:recastCard(event:getCostData(self).cards, player, dianshou.name)
  end,
})

return dianshou
